
let fetchMemberListResult=null;

app.get("/fetchMemberList",async (req,res)=>{
    let params=req.params;
    fetchMemberListResult=await window.M.request.get("/statistics/memberList",params);
    res.send(fetchMemberListResult);
});






class StatisticsMemberList extends BaseTableList  {
    constructor(props) {
        super(props);
        this.fixedKey="id";
        this.filter = [ 'level'];
        this.actionWidth=300;
        this.placeholder="请输入姓名或公司";
        this.actioncolumns = false;
        this.canRowSelection=false;

        this.columns=this.getColumns();
    }

    state = {
        data: [],
        vip_group_id:-1,
        pagination: {   //搜索参数
            current: 1,
            pageSize: 20,
            keywords: '',
            order: '',
            startDate: (new Date().getFullYear()-1)+"-10-01",
            filter: {  level: ''},
            total: 0,
            showSizeChanger: true,
            pageSizeOptions: ['20','200','500'],
            showTotal: ((total) => {
                return `共 ${total} 条`;
            }),
            onShowSizeChange: (current, pageSize) => {
                const { pagination } = this.state;
                pagination.pageSize = pageSize;
                this.setState({
                    pagination
                });
            }
        },
        loading: false,
        visible: false,
        modalText: '确定删除？',
        selectedRowKeys: [],
        selectedRows: [],
    }

    res_data = {
        name: {
            label: '姓名',
            width: 100
        },
        company: {
            label: '公司',
            width: 160
        },
        yb_score: {
            label: '元宝分',
            width: 100
        },
        lever_score: {
            label: '会员类型',
            width: 100
        },
        sign_count:{
            label: '签到次数',
            width: 100
        },
        ea_answer_count:{
            label: '轻松问答次数',
            width: 100
        },
        quiz_answers_count:{
            label: '涨跌次数',
            width: 100
        },
        exchange_count:{
            label: '兑换次数',
            width: 100
        }
    }



    async componentDidMount() {

        this.fetch();
    };



    getColumns(){
        let res_data = this.res_data;
        let columns = [];
        let tableWidth = this.tableWidth;
        for(let key in res_data){
            tableWidth += res_data[key]['width'];
            let o = {
                title: res_data[key].label,
                dataIndex: key,
                key: key,
                width: res_data[key]['width'],
                sorter: (a, b) => a[key]-b[key],
                render: (text, row, index) => {
                    return this.viewRender(key, res_data, text, row, index);
                }
            };
            if(key=="index"){
                o = {
                    title: res_data[key].label,
                    dataIndex: key,
                    key: key,
                    width: res_data[key]['width'],
                    sorter: (a, b) => a[key]-b[key],
                    render: (text, row, index) => {
                        return <div>{index}</div>;
                    }
                };
            }
            if(key=="lever_score"){
                o = {
                    title: res_data[key].label,
                    dataIndex: key,
                    key: key,
                    width: res_data[key]['width'],
                    sorter: (a, b) => a.lever_score-b.lever_score,
                    render: (text, row, index) => {
                        return <div>{row.score_type}({row.lever_score})</div>;
                    }
                };
            }
            columns.push(o);
        }
        if(this.actioncolumns){
            columns.push({
                title: '操作',
                key: 'operation',
                fixed: 'right',
                width: this.actionWidth,
                render: (text, row, index) => {
                    return this.actionRender(text, row, index);
                },
            });
        }
        this.tableWidth=tableWidth;
        return columns;
    }



    filterContent(){
        const { pagination} = this.state;
        if(JSON.stringify(pagination.filter)=='{}') return <div></div>;
        const level = ['白银会员', '黄金会员', '铂金会员', '钻石会员'];
        return <div>
            <div style={{padding: '5px 0px 5px 0px'}}>
                <span style={{fontWeight: 'bolder'}}>{"会员等级："}</span>
                <CheckboxGroup options={level} value={pagination.filter.level.split(',')} onChange={(v) => this.filterType('level',v)} />
            </div>
        </div>
    }


    //点击排序
    vipGroupChange(v){
        this.setState({
            vip_group_id: v
        });
    }


    inputRender(){
        const {pagination, selectedRows, selectedRowKeys } = this.state;
        let defaultDate=   new Date();
        defaultDate.setFullYear(new Date().getFullYear()-1);
        defaultDate.setMonth(9);
        defaultDate.setDate(1);
        return <div>
            <Form style={{"display":"flex",padding: "24px 0 0 24px"}}>
                <div style={{flex: 1,display:  'flex'}}>
                    <Popover placement={'bottomLeft'} content={this.filterContent()} trigger="hover">
                        <Button style={{"marginRight": 15,"top": 4}}>{"筛选"}</Button>
                    </Popover>
                    <Form.Item>
                        <Input  name="keywords1" style={{width: 300}} placeholder={this.placeholder} defaultValue={pagination.keywords}/>
                    </Form.Item>
                    <Form.Item>
                        <label>起始时间:</label>
                        <DatePicker
                            format="YYYY-MM-DD"
                            defaultValue={moment(defaultDate)}
                            onChange={
                                v=>{
                                    this.state.pagination.startDate=v._d.format("yyyy-MM-dd");
                                }}>

                        </DatePicker>
                    </Form.Item>
                    <Button type="primary" onClick={this.handleSearch} style={{"position":"relative","left":15,"top":3}}>搜索</Button>

                    <Button  type="primary" onClick={async ()=>{
                        await window.M.request.get("/statistics/memberReset",{startDate:this.state.pagination.startDate});
                        fetchMemberListResult=null;
                        this.handleSearch();
                    }} style={{"position":"relative","left":50,"top":3}}>刷新</Button>


                </div>


            </Form>
            <div style={{position: 'relative',top: -15,left: 25}}>
                {
                    this.tagsRender()
                }
            </div>
        </div>
    }
    //搜索
    handleSearch(){
        let keywords = $('input[name=keywords1]').val();
        let pagination = this.state.pagination;
        pagination.keywords = keywords;
        pagination.current = 1;
        this.setState({
            pagination,
            selectedRowKeys: [],
            selectedRows: [],
        },() => {
            this.fetch();
        });
    }
    edit=async row=>{
        let inputRef=null;
        let that=this;
        await new Promise(resolve => {
            Modal.confirm({
                icon: <span></span>,
                title: '编辑',
                width:1000,
                content:
                    <Input.TextArea ref={r=>{
                        inputRef=r;
                    }
                    } defaultValue={row.cmd} rows={5} cols={10} />
                ,
                async onOk() {
                    let value=   inputRef.textAreaRef.value
                    let r= await MIO.configUpdate({id:row.id,useData:value});
                    if(M.checkR(r)){
                        that.fetch();
                    }
                    resolve();
                },
                onCancel() { },
            });
        });
    }


    //新增
    handleCreate(){

    }

    //获取数据
    async fetch() {
        this.setState({ loading: true });
        let { current,pageSize,keywords,order,filter,startDate } = this.state.pagination;
        let r=await window.MIO.fetchMemberList({
            page: current,
            num: pageSize,
            order: order,
            keywords: keywords,
            startDate:startDate,
            filter: JSON.stringify(filter)
        });
        let total =r.data.total;
        const pagination = { ...this.state.pagination };
        pagination.total = total;
        this.setState({
            pagination,
            data: r.data.dataList,
            loading: false
        })
    }




    actionRender(text, row, index) {
        return(
            <p>
            </p>
        )
    }

    getTotalNum(){


        return <div>
            共{this.state.data.length}
        </div>
    }

    render() {
        return (
            <div>
                {this.inputRender()}
                {this.getTotalNum()}
                <Table
                    rowKey={record => record.id}
                    columns={this.columns}
                    dataSource={this.state.data}
                    pagination={this.state.pagination}
                    loading={this.state.loading}
                    scroll={{ x: this.tableWidth, y: 1000 }}
                    onRowClick={this.handleTableClick}
                    onChange={this.handleTableChange} />
            </div>
        )
    }
}
